var IwJwUtils = (function () {
    var _Ajax = function () {
        var ajax = function (options) {
            var _options = $.extend({}, {
                async: true, //默认为true 异步
                type: 'POST', url: '', data: {}, callback: function () {
                }, dataType: 'json', beforeSend: function () {
                    return true;
                }
            }, options);
            $.ajax({
                async: _options.async, //默认为true 异步
                type: _options.type,
                url: _options.url,
                data: _options.data,
                dataType: _options.dataType,
                beforeSend: function () {
                    if (_options.beforeSend) {
                        return _options.beforeSend();
                    }
                    return true;
                },
                success: function (data) {
                    _options.callback && _options.callback(data);
                },
                error: function (data) {
                    validateReqStatus(data);
                }
            });
        };
        return {
            sendPost: function (options) {
                options.type = 'POST';
                ajax(options);
            },
            sendGet: function (options) {
                options.type = 'GET';
                ajax(options);
            }
        }
    }();
    var _Msg = function () {
        var msg = function (options) {
            var _options = $.extend({}, {
                msg: '', time: 2000, callback: function () {
                }
            }, options);
            layer.msg(_options.msg, {time: _options.time, icon: _options.icon}, function () {
                _options.callback && _options.callback();
            });
        }
        return {
            def: function (options) {
                msg(options);
            },
            success: function (options) {
                options.icon = 1;
                msg(options);
            },
            error: function (options) {
                options.icon = 2;
                msg(options);
            },
            warning: function (options) {
                options.icon = 7;
                msg(options);
            },
            happy: function (options) {
                options.icon = 6;
                msg(options);
            },
            sorry: function (options) {
                options.icon = 5;
                msg(options);
            }
        };
    }();
    var _Alerts = function () {
        var _alert = function (options) {
            var _options = $.extend({}, {
                title: '信息',
                msg: '',
                skin: 'layui-layer-demo',
                closeBtn: 2, width: 220, height: 160,
                btn: ['确定', '取消'],
                yes: function () {
                },
                cancel: function () {
                }
            }, options);
            layer.open({
                title: _options.title,
                type: 1,
                content: '<div style="padding: 20px;text-align: center;">' + _options.msg + '</div>',
                skin: _options.skin,
                area: [_options.width + 'px', _options.height + 'px'],
                btn: _options.btn,
                yes: function (idx) {
                    _options.yes && _options.yes(idx);
                },
                cancel: function () {
                    _options.cancel && _options.cancel();
                }
            });
        }

        var _alertByUrl = function (options) {
            var _options = $.extend({}, {
                url: '',
                skin: 'layui-layer-demo',
                width: 200,
                height: 200,
                closeBtn: 2,
                btn: ['确定', '取消'],
                yes: function () {
                },
                cancel: function () {
                }
            }, options);
            layer.open({
                type: 2,
                width: _options.width,
                height: _options.height,
                area: [_options.width, _options.height],
                skin: _options.skin, //加上边
                content: _options.url,
                btn: _options.btn,
                yes: function (idx, obj) {
                    _options.yes && _options.yes(idx, obj);
                },
                cancel: function () {
                    _options.cancel && _options.cancel();
                }
            });
        }
        return {
            def: function (options) {
                _alert(options);
            },
            alertByUrl: function (options) {
                _alertByUrl(options);
            }
        }
    }();
    var _Tips = function () {
        var tip = function (options) {
            var _options = $.extend({}, {direction: 3, color: '#3595CC', time: 2000}, options);
            layer.tips(_options.msg, _options.$target, {
                tips: [_options.direction, _options.color],
                time: _options.time
            });
        };
        return {
            def: function (options) {
                options.direction = 2;
                tip(options);
            },
            up: function (options) {
                options.direction = 1;
                tip(options);
            },
            right: function (options) {
                options.direction = 2;
                tip(options);
            },
            down: function (options) {
                options.direction = 3;
                tip(options);
            },
            left: function (options) {
                options.direction = 4;
                tip(options);
            }
        }
    }();
    var _getFileSize = function (file, limit) {
        // MB
        var size = (file.get(0).files[0].size / 1024 / 1024);
        if (!limit) {
            return size;
        }
        if (size <= limit) {
            return true;
        }
        _Msg.error({msg: "请选择小于或等于" + limit + "M的图片"});
        return false;
    };
    var _checkImg = function (id) {
        var tmp = $("#" + id).val();
        if (tmp == '' || tmp == null) {
            _Msg.error({msg: "请选择图片(jpeg|jpg|png|gif|bmp)"});
            return false;
        } else {
            if (!/\.(jpeg|jpg|png|gif|bmp|JPEG|JPG|PNG|GIF|BMP)$/.test(tmp)) {
                _Msg.error({msg: "请选择图片(jpeg|jpg|png|gif|bmp)"});
                $("#" + id).val("");
                return false;
            }
            return true;
        }
    };
    var _Confirm = function () {
        return {
            def: function (tip,yes,cancel) {
                layer.confirm(tip, {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    yes && yes();
                }, function(){
                    cancel && cancel();
                });
            }
        }
    }();
    var _Loadings = function () {
        return {
            def: function () {
                return layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
            }
        }
    }();
    var _Close = function (index) {
        layer.close(index);
    };
    return {
        Ajax: _Ajax,
        Messages: _Msg,
        Alerts: _Alerts,
        Tips: _Tips,
        Loadings: _Loadings,
        Close: _Close,
        getFileSize: _getFileSize,
        checkImg: _checkImg,
        Confirm: _Confirm,
    }
})();